home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / GDOpenWindow.c < prev    next >
Text File  |  1996-03-08  |  18KB  |  409 lines

  1. /* GDOpenWindow.c
  2. Copyright © 1989-1995 Denis G. Pelli
  3.  
  4. EXAMPLE:
  5.  
  6. Open your window by saying:
  7.     window=GDOpenWindow(device);
  8. When you're through with the window, get rid of it by calling:
  9.     GDDisposeWindow(window);
  10. Besides closing the window and disposing of the allocated memory structures
  11. GDDisposeWindow also restores the device's color table and clut.
  12.  
  13. SUMMARY:
  14.  
  15. window=GDOpenWindow(device) opens a full-screen window on the specified screen,
  16. call GDGrayColorTable(device) which gives the device a standard gray color table
  17. (if it doesn't already), and calls AddExplicitPalette(window), which allows you
  18. to specify colors to paint with by using PmForeColor() and PmBackColor(). The
  19. window truly fills the screen: any rounded corners are made square and if the
  20. window is on the main screen then the Menu Bar is hidden. The screen occupies
  21. the content area of the window; the window's frame (one-pixel black line border
  22. and title bar) is off that screen, but will appear on other screens that form
  23. contiguous parts of your desktop. The elegant solution to this, which perhaps
  24. someone will implement, would be to define a new window type that has no frame.
  25.  
  26. GDDisposeWindow(window) closes and discards a color window, disposing of any
  27. palette or color table, generally undoing whatever GDOpenWindow1 did.
  28.  
  29. AddExplicitPalette(window) adds a palette to a color window or GWorld with all
  30. the colors marked as pmExplicit. This allows you to use PmForeColor() and
  31. PmBackColor() to specify the value you want poked into each pixel by QuickDraw
  32. operations, e.g. EraseRect() and DrawString().
  33.  
  34. RemovePalette(window) disposes of the palette created by AddExplicitPalette.
  35.  
  36. SwapWindowExplicitMode(window,&explicit) swaps the contents of the Boolean
  37. argument with the "explicit" bit (bit 14 of the ctFlags field) in the color
  38. table. The use of this bit is documented in "VideoToolbox:Notes:CopyBits
  39. slavishly". After much experimentation I decided that it's not useful, but I
  40. leave this routine here for others that wish to experiment. Note that when
  41. GDOpenWindow() creates a window, the window shares the color table belonging to
  42. the device, so calling SwapWindowExplicitMode() modifies the shared table, i.e.
  43. the device's color table is modified. You should restore it by calling
  44. MakeColorTableExplicit() again. Failing to restore it will have cosmetic
  45. aftereffects, even after your application quits. The most obvious aftereffect is
  46. that documents in Word will be largely gray.
  47.  
  48. GetBitMapPtr(window) returns a pointer to the window's bitmap or pixmap.
  49.  
  50. IsGWorldPtr(window) returns true only if the window is a GWorldPtr.
  51.  
  52. COMMENTS:
  53.  
  54. Please read "VideoToolbox:Notes:CopyBits slavishly". QuickDraw likes to
  55. pick a bunch of good colors and stuff them in the clut, in essentially random
  56. order, except that white is first and black is last. It wants you to specify any
  57. desired color as an RGB triplet and then it picks the mysterious clut index that
  58. would provide the closest match. If you're processing grayscale images, then
  59. QuickDraw's approach involves a lot of overhead involving inverse color tables,
  60. and makes the numbers stored in your pixels meaningless (unless you look them up
  61. in the associated color table or palette).
  62.  
  63. The philosophy of the VideoToolbox is to bypass QuickDraw's color model, and
  64. work explicitly with the numbers that are stored in your pixels.
  65. SetPixelsQuickly will efficiently poke (or peek) the numbers in your PixMap.
  66. However, if you want to use QuickDraw's handy drawing operations, especially
  67. EraseRect() and DrawString() then you need a way to specify the foreground and
  68. background colors. AddExplicitPalette() gives your window a palette in which all
  69. the colors are marked as pmExplicit. This tells the palette manager to use your
  70. arguments to PmForeColor() and PmBackColor() literally.
  71.  
  72. I suggest that you control the clut by calling GDUncorrectedGamma() and
  73. GDSetEntries(), since these calls directly control the video device driver,
  74. bypassing the Color Manager. Since the Color and Palette Managers don't know
  75. that you've changed the color environment they can't react to it, and will
  76. passively let you continue to specify colors by their clut index. For example,
  77. the Palette Manager religiously believes that the first clut entry should be
  78. white and the last one black, and it will change them back to those values if
  79. you change them and it finds out about it. If you use the Color Manager
  80. SetEntries call then the Paletter Manger WILL find out because a record is made
  81. in the ColorTable. Calling GDSetEntries() bypasses the Color Manager. Instead,
  82. the video device driver writes directly to the clut and the ColorTable is not
  83. modified. Of course, this means that you should ignore the ColorTable since it
  84. will no longer reflect the contents of the clut.
  85.  
  86. Every time you access the stdio package, e.g. printf or getch(), THINK C will
  87. move the console window to the front, which may obscure your window. You can
  88. bring your window back to the front by calling BringToFront() or SelectWindwo().
  89.  
  90. NOTE:
  91. Tom Busey (busey@indiana.edu) writes: "I'm using an 8-bit SuperMac ColorCard
  92. that a used computer dealer gave me when I ordered a Toby card. I discovered
  93. that the status and control calls for GDUncorrectedGamma return -17 and -18
  94. respectively, which means that the driver doesn't support different gammas.
  95. GDUncorrectedGamma returns an error message, but GDOpenWindow doesn't use the
  96. error message or pass it back to the calling program. I'm wondering if there are
  97. people using GDOpenWindow who think that they are getting a linear gamma but who
  98. are actually getting an error message and not learning about it."
  99.      Tom's facts are right, but he needn't worry. A few video drivers, e.g.
  100. Radius PowerView, and apparently the SuperMac ColorCard, don't implement gamma
  101. tables at all, but the error from GDUncorrectedGamma is probably not a concern.
  102. The real test is to run TimeVideo. TimeVideo does a write-and-read-back test of
  103. the clut of your video card. If that test passes then you can safely conclude
  104. that there's no gamma translation going on. So, as nearly every document in the
  105. VideoToolbox says: run TimeVideo and read the report.
  106.      Here's how gamma tables work. According to the Apple manual (Designing
  107. Cards and Drivers), the values in the rgb triplets that you supply in a
  108. cscSetEntries call to the video driver are first translated via the gamma table
  109. (each r,g, and b component separately) before being loaded into the CLUT
  110. hardware table. Most drivers maintain a gamma table internally (in computer
  111. memory) and allow you to get and set it via the GDGetGamma and GDSetGamma calls.
  112. A few video drivers (Radius PowerView, SuperMac ColorCard) don't support gamma
  113. tables. They load your rgb values directly into the CLUT, without translation.
  114. However, for users of the VideoToolbox that's usually fine, since that's exactly
  115. the behavior that we routinely request by calling GDUncorrectedGamma. Of course,
  116. if you want to load a custom gamma table, other than the identity
  117. transformation, then you'll be calling GDSetGamma, and you should make sure it
  118. does not return an error. (Alas, the Radius PowerView driver more or less
  119. ignores the set- and get-gamma requests--GDGetGamma returns a table that's all
  120. zeroes--but the driver fails to report an error. I wrote to the Radius
  121. programmer a year ago, but they're no longer interested in working on that
  122. product.)
  123.      
  124.  
  125. PROBLEM:
  126. How can I tell whether I've got a CGrafPtr or a GWorldPtr? (In one case I'll call 
  127. GetGWorldDevice, in the other case I'll get max device.)
  128. I noticed that the portVersions are 0xc001 and 0xc000. Should I use that to tell?
  129.  
  130. ANSWER FROM APPLE DEVSUPPORT (8/93):
  131. You can use GetGWorldDevice() on GWorldPtr, GrafPort or CGrafPort. Use
  132. TestDeviceAttibute() to test if the returned GDHandle is a "real" screen or not.
  133.  
  134. Also, you should refer to the tech note “RowBytes Revealed II” (available on the
  135. June Developer CD) which states:
  136.  
  137. You'll notice in Figure 1 that the portVersion field of a cGrafPort coincides
  138. with the location of the rowBytes field of a grafPort. Remember, a cGrafPort has
  139. the same size as a grafPort. During debugging, you can use the same information
  140. that CopyBits uses to identify cGrafPorts.
  141.  
  142. If you use a grafPort template to display memory for an unknown grafPort, you
  143. can tell if it is a cGrafPort because the rowBytes will be equal to 0xC000. The
  144. 0xC corresponds to the two high bits being set in the portVersion field of a
  145. cGrafPort. Since these bits can not be set in a grafPort, you know you have a
  146. cGrafPort. In addition, if the bottom bit of the portVersion field is set, then
  147. it is a gWorld. Thus, if your rowBytes field has a value of 0xC001, then you
  148. know you have a gWorld.
  149.  
  150. CONCLUSION:
  151. This advice is implemented in the routine IsGWorldPtr(window).
  152.  
  153. HISTORY:
  154.  
  155. 12/88        dgp    wrote it
  156. 8/5/89            added call to GDUncorrectedGamma, so I couldn't forget.
  157. 8/15/89     dgp trivia
  158. 3/20/90        dgp    make compatible with MPW C.
  159. 3/29/90        dgp    changed declared returned type from WindowPtr to CWindowPtr, which
  160.                 is what it's really been all along. Same change to argument of
  161.                 GDDisposeWindow(). The new offscreen GWorld calls for the first time
  162.                 make it easier to honestly declare one's windows as color rather
  163.                 than pretending they're not.
  164. 8/24/91        dgp    Made compatible with THINK C 5.0.
  165. 2/1/92        dgp Made optional the device argument to GDDisposeWindow(). If it's
  166.                 NULL, then it will be determined automatically from window.
  167. 2/3/93        dhb    Extracted AddExplicitPalette from GDOpenWindow.
  168. 2/21/93        dgp    HideMenuBar if window is on main screen.
  169. 2/23/93        dgp AddExplicitPalette() returns immediately unless it receives a color 
  170.                 window.
  171.                 Added GDOpenWindow1() and GDDisposeWindow1(), which both use
  172.                 a WindowPtr, instead of a less convenient CWindowPtr.
  173. 3/5/93        dgp    Added calls to UnclipScreen() and RestoreScreenClipping(), so the window
  174.                 now truly fills the whole screen. Edited GDOpenWindow() for clarity.
  175. 3/7/83        dgp    Added calls to GDSaveGamma(device) and GDRestoreGamma(device).
  176. 4/16/93        dgp    Cosmetic editing.
  177. 5/22/93        dgp    Added RemovePalette(), but didn't test it.
  178. 1/27/94        dgp    Cosmetic editing.
  179. 3/5/94        dgp    Decided that RemovePalette() works fine now.
  180. 6/8/94        dgp AddExplicitPalette now calls the new MakeColorTableExplicit(), which
  181.                 sets the Color Table ctFlags to indicate that
  182.                 the Color Table refers explicitly
  183.                 to the explicit palette. This tells CopyBits to treat
  184.                 the pixels as numbers, not indices.
  185. 6/13/94        dgp In response to query by David Brainard I now document above
  186.                 the obscure fact that GDOpenWindow's frame will appear on
  187.                 other screens that are contiguous parts of the desktop.
  188. 8/11/94 dhb & dgp David Brainard discovered that MakeColorTableExplicit(), which
  189.                 is called by AddExplicitPalette, which is called by GDOpenWindow1, was affecting
  190.                 not just the new window's color table, but was also affecting the device's color
  191.                 table (because they share the same color table), and that the device's color
  192.                 table was not restored. "The symptom is that if you subsequently launch
  193.                 Microsoft Word, the background of the text window takes on a strange color
  194.                 (grey) in regions where there is text," and the problem persists until reboot.
  195.                 The solution is to give our window its own color table, which we can
  196.                 safely modify. 
  197. 8/12/94 dhb & dgp on 8/11 denis inadvertently introduced a bug into GDDisposeWindow1, 
  198.                 moving the call to GetWindowDevice() to a point after the window
  199.                 was disposed of. David fixed it, moving the call back where it belonged.
  200. 8/14/94    dgp        GDOpenWindow1 now gets the color table from the window's device instead
  201.                 of the main device.
  202. 8/17/94        dgp make color table NOT explicit when GDDisposeWindow1 is called.
  203. 9/5/94         dgp removed assumption in printf's that int==short.
  204. 10/10/94        dgp GDOpenWindow1 now associates correct device's color table with window,
  205.                 not MainDevice's.
  206. 10/24/94    dgp GDOpenWindow1 returns NULL if there's no driver associated with the device.
  207. 10/25/94    dgp Added GDGrayColorTable(device), which gives the device a standard gray color table.
  208. 11/2/94        dgp Leave the explicit bit alone. For the adventurous I provide the 
  209.                 MakeColorTableExplicit() routine and documentation in 
  210.                 VideoToolbox:Notes:"CopyBits slavishly".
  211. 11/17/94    dgp    renamed IsWindow to IsGrafPtr. Renamed IsOffScreen to IsGWorldPtr.
  212. 4/11/95        dgp Fixed assertion failure in GDGrayWindow; the rsrc id of the 2-bit gray ramp is 2,
  213.                 not 32+2.
  214. 4/11/95    dgp        transferred some documentation of IsGWorldPtr from GetScreenDevice.c to above.
  215. 3/8/95    dgp        GDOpenWindow1()'s call to GDGrayColorTable() causes the 
  216.             window frame to become orange. I don't understand why, but it's only a cosmetic bug, 
  217.             so i'm ignoring it for the moment.
  218. */
  219. #include "VideoToolbox.h"
  220.  
  221. #define MAKE_COLOR_TABLE 0
  222.  
  223. WindowPtr GDOpenWindow1(GDHandle device)
  224. {
  225.     return (WindowPtr)GDOpenWindow(device);
  226. }
  227.  
  228. void GDDisposeWindow1(WindowPtr window)
  229. {
  230.     GDDisposeWindow((CWindowPtr) window);
  231. }
  232.  
  233. CWindowPtr GDOpenWindow(GDHandle device)
  234. {
  235.     CWindowPtr window;
  236.     Rect r;
  237.     GDHandle oldDevice;
  238.  
  239.     if(device==NULL || (*device)->gdRefNum==0)return NULL;
  240.     r=(*device)->gdRect;        // rect of desired screen in global coordinates
  241.     GDSaveGamma(device);
  242.     GDUncorrectedGamma(device);
  243.     UnclipScreen(device);
  244.     GDGrayColorTable(device);
  245.     oldDevice=GetGDevice();
  246.     SetGDevice(GetMainDevice());    // Needed for window to work properly.
  247.     window=(CWindowPtr)NewCWindow(NULL,&r,"\p",TRUE,plainDBox,(WindowPtr) -1L,0,123L);
  248.     (*window->portPixMap)->pmTable=(**(**device).gdPMap).pmTable;
  249.     SetGDevice(oldDevice);
  250.     AddExplicitPalette((WindowPtr)window);
  251.     return window;
  252. }
  253.  
  254. void GDGrayColorTable(GDHandle device)
  255. // give the device a standard gray color table
  256. {
  257.     ColorTable **table;
  258.     PixMap **pm;
  259.     int id;
  260.  
  261.     if(device==NULL)return;
  262.     pm=(**device).gdPMap;
  263.     table=(**pm).pmTable;
  264.     id=(**pm).pixelSize;
  265.     if(id>2)id+=32;
  266.     if((**device).gdType==clutType && (**table).ctSeed!=id){
  267.         // Give the device a standard gray-ramp color table
  268.         table=GetCTable((**pm).pixelSize+32);    // gray ramp
  269.         assert(table!=NULL);
  270.         assert((**(**pm).pmTable).ctSize==(**table).ctSize);
  271.         memcpy(*(**pm).pmTable,*table,8+((**table).ctSize+1)*sizeof(ColorSpec));
  272.         DisposeHandle((Handle)table);
  273.         table=(**pm).pmTable;
  274.         (**table).ctFlags|=0x8000;    // mark as belonging to a device
  275.         GDeviceChanged(device);
  276.     }
  277. }
  278.  
  279. void GDDisposeWindow(CWindowPtr window)
  280. // Dispose of window and palette and restore the screen's clut and clipping.
  281. {
  282.     GDHandle device;
  283.     
  284.     if(window==NULL)return;
  285.     RemovePalette((WindowPtr)window);
  286.     #if MAKE_COLOR_TABLE
  287.         DisposHandle((Handle)(*((CGrafPtr)window)->portPixMap)->pmTable);
  288.     #endif
  289.     device=GetWindowDevice((WindowPtr)window);
  290.     DisposeWindow((WindowPtr)window);
  291.     if(device==NULL)return;
  292.     GDRestoreGamma(device);
  293.     GDRestoreDeviceClut(device);
  294.     RestoreScreenClipping(device);
  295. }
  296.  
  297. void AddExplicitPalette(WindowPtr window)
  298. // Create a palette for the color window and mark all the entries as explicit.
  299. // Copy the entries from the window's device.
  300. {
  301.     GDHandle device;
  302.     CTabHandle ct;
  303.     PaletteHandle palette;
  304.     int colors;
  305.     #if MAKE_COLOR_TABLE
  306.         int i,error;
  307.     #endif
  308.  
  309.     if(window==NULL)return;
  310.     if(((CGrafPtr)window)->portVersion>=0) return;    // Not a color window, return.
  311.     device=GetWindowDevice(window);
  312.     if(device==NULL)PrintfExit(
  313.         "AddExplicitPalette: window has no device! %s line %d.\n",__FILE__,__LINE__);
  314.     ct=(**(**device).gdPMap).pmTable;
  315.     colors=(*ct)->ctSize+1;
  316.     #if MAKE_COLOR_TABLE
  317.         error=HandToHand((Handle *)&ct);
  318.         if(error)PrintfExit("%s line %d: error %d in copying color table\n"
  319.             ,__FILE__,__LINE__,error);
  320.         (*ct)->ctFlags &= 0x7fff;    // clear "device" bit
  321.         for(i=0;i<colors;i++) (*ct)->ctTable[i].value=i;
  322.         (*ct)->ctSeed=GetCTSeed();
  323.         (*((CGrafPtr)window)->portPixMap)->pmTable=ct;
  324.     #endif
  325.     palette=NewPalette(colors,ct,pmExplicit+pmTolerant,0);
  326.     SetPalette(window,palette,0);
  327. }
  328.  
  329. /*
  330. RemovePalette may look dangerous. I worried that the Window or
  331. Palette Manager might become confused if it later tries to access the
  332. window's palette, which no longer exists. However, by trial and error
  333. I've found that this works fine, and that my attempts to make explicit the
  334. fact that the palette is gone, e.g. by calling SetPalette(window,NULL,0),
  335. all produced crashes. I conclude that the Palette or Window Manager monitors
  336. the calls to DisposePalette and thus knows that the palette is gone.
  337. */
  338. void RemovePalette(WindowPtr window)
  339. {
  340.     if(window==NULL)return;
  341.     DisposePalette(GetPalette(window));
  342. }
  343.  
  344. void SwapWindowExplicitMode(CWindowPtr window,Boolean *explicit)
  345. // Get and set bit 14 of the ctFlags field of the window's color table.
  346. // This is only relevant to a CopyBits from an offscreen
  347. // graphics environment, be it PixMap or GWorld, to an onscreen window.
  348. // See Inside Mac VI, page 20-17. To get the full story see "CopyBits slavishly" in 
  349. // VideoToolbox:Notes.
  350. {
  351.     int i,ok;
  352.     ColorTable **ct;
  353.     Boolean oldExplicit;
  354.  
  355.     if(window==NULL)return;
  356.     // If it's a GWorldPtr then we must lock the pixels to access the pixmap.
  357.     if((window->portVersion&0xc001)==0xc001){
  358.         ok=LockPixels(GetGWorldPixMap(window));
  359.         if(!ok)return;
  360.     }
  361.     ct=(*window->portPixMap)->pmTable;
  362.     oldExplicit=((*ct)->ctFlags&0x4000)!=0;
  363.     if(*explicit){
  364.         (*ct)->ctFlags|=0x4000;
  365.         for(i=(*ct)->ctSize;i>=0;i--)(*ct)->ctTable[i].value=i;
  366.     }else{
  367.         (*ct)->ctFlags&=~0x4000;
  368.     }
  369.     *explicit=oldExplicit;
  370. }
  371.  
  372. // CAUTION: The PixMap handles are not locked. So don't move memory. The
  373. // returned pointers will become invalid as soon as you call any routine
  374. // that moves memory. CopyBits and CopyBitsQuickly are ok.
  375. BitMap *GetBitMapPtr(CWindowPtr window)
  376. {
  377.     BitMap *bits;
  378.     int ok;
  379.     PixMapPtr *pm;
  380.  
  381.     if(IsGWorldPtr(window)){
  382.         // It's a GWorldPtr, lock the pixels, return pixmap ptr.
  383.         ok=LockPixels(GetGWorldPixMap((GWorldPtr)window));
  384.         if(!ok)return NULL;
  385.         pm=GetGWorldPixMap((GWorldPtr)window);
  386. //        HLock((Handle)pm);
  387.         bits = (BitMap *)*pm;
  388.     }else if(!IsGrafPtr(window)){
  389.         pm=window->portPixMap;
  390. //        HLock((Handle)pm);
  391.         bits = (BitMap *)*pm;
  392.     }else{
  393.         bits = &((WindowPtr)window)->portBits;
  394.     }
  395.     return bits;
  396. }
  397.  
  398. Boolean IsGWorldPtr(CWindowPtr window)    // Is it a GWorldPtr?
  399. {
  400.     return (window->portVersion&0xc001) == 0xc001;
  401. }
  402.  
  403.  
  404. Boolean IsGrafPtr(CWindowPtr window)    // Is it a WindowPtr?
  405. {
  406.     return window->portVersion>=0;
  407. }
  408.  
  409.